package com.cloudwise.monitoring_system.jmx.testDemo;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author Reisen
 * @description:测试堆内存和非堆内存占用
 * @date 2021/5/27 10:56
 */
public class MemDemo {
    private static NumberFormat fmtI = new DecimalFormat("###,###", new DecimalFormatSymbols(Locale.ENGLISH));

    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();

        double heapMemoryUsageUsed = bytesFormat(heapMemoryUsage.getUsed());
        System.out.println(heapMemoryUsageUsed);

        double nonHeapMemoryUsageUsed = bytesFormat(nonHeapMemoryUsage.getUsed());
        System.out.println(nonHeapMemoryUsageUsed);






    }

    private static double bytesFormat(long bytes){
        return (double)bytes/1024/1024;
    }
}
